package red.internal.core.message;

/**
 * @author Jin Zheng
 * @since 2019-05-03
 */
public class CacheMessage extends Message
{
	private CacheState state;
	private boolean compress = false;
	private CacheCommand command;
	private String key;
	private byte[] value;
	private long ttl = 0L;

	public CacheMessage()
	{
	}

	public static CacheMessage createPersistence(CacheCommand command, String key)
	{
		return create(CacheState.PERSISTENCE, command, key);
	}

	public static CacheMessage createTransient(CacheCommand command, String key)
	{
		return create(CacheState.TRANSIENT, command, key);
	}

	public static CacheMessage create(CacheState state, CacheCommand command, String key)
	{
		CacheMessage message = Message.create(Protocol.CACHE);
		message.setState(state);
		message.setCommand(command);
		message.setKey(key);
		return message;
	}

	public CacheMessage toResponse(Code code, String message)
	{
		CacheMessage response = this.createResponse(message);
		response.setState(state);
		response.setCompress(compress);
		response.setCommand(command);
		response.setCode(code);
		return response;
	}

	protected CacheMessage toResponse(boolean success, String message)
	{
		Code code = success ? Code.SUCCESS : Code.CACHE;
		return this.toResponse(code, message);
	}

	public void setState(int state)
	{
		this.state = CacheState.valueOf(state);
	}

	public void setCommand(int command)
	{
		this.command = CacheCommand.valueOf(command);
	}

	public CacheState getState()
	{
		return state;
	}

	public void setState(CacheState state)
	{
		this.state = state;
	}

	public boolean isCompress()
	{
		return compress;
	}

	public void setCompress(boolean compress)
	{
		this.compress = compress;
	}

	public CacheCommand getCommand()
	{
		return command;
	}

	public void setCommand(CacheCommand command)
	{
		this.command = command;
	}

	public String getKey()
	{
		return key;
	}

	public void setKey(String key)
	{
		this.key = key;
	}

	public byte[] getValue()
	{
		return value;
	}

	public void setValue(byte[] value)
	{
		this.value = value;
	}

	public long getTtl()
	{
		return ttl;
	}

	public void setTtl(long ttl)
	{
		this.ttl = ttl;
	}
}
